package com.atqinke.designpattern.creationtype.factoryPlus;

import com.atqinke.designpattern.creationtype.factory.pojo.Tea;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @BelongsProject: BasicConnection
 * @BelongsPackage: com.atqinke.DesignPattern.creation.factoryPlus
 * @Author: NieHk
 * @CreateTime: 2022-08-25  22:55
 * @Description: 工厂加配置文件
 * @Version: 1.0
 */
public class TeaFactoryPlus {

    //奶茶容器
    private static Map<String, Tea> map = new HashMap<>();

    static {
        //根据配置文件创造奶茶放到容器中
        Properties ps = new Properties();
        InputStream is = TeaFactoryPlus.class.getClassLoader().getResourceAsStream("tea.properties");
        try {
            ps.load(is);
            Set<Object> keys = ps.keySet();
            for (Object key : keys) {
                String classname = ps.getProperty((String) key);
                Class clazz = Class.forName(classname);
                Tea tea = (Tea) clazz.newInstance();
                map.put((String) key,tea);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 从容器中获取奶茶
     * @param tea
     * @return
     */
    public static Tea getTea(String tea){
        return map.get(tea);
    }

}
